package com.kitty.game.fight.bean;

import com.kitty.game.config.XinJun;
import com.kitty.common.utils.PositionUtils;
import com.kitty.game.fight.bean.FightObject;
import lombok.Getter;
import org.nutz.json.Json;

import java.util.ArrayList;
import java.util.Random;

public class TowerFightObject extends FightObject {

    @Getter
    protected XinJun xinjun;

    @Override
    public int getTao(){
        return Integer.parseInt(xinjun.getTao_effect());
    }

    public TowerFightObject(int monsterId, XinJun xinJun, int layer, short npcIcon, int index, int hostId, short level) {
        setPos(PositionUtils.positions.get(index));
        setName(xinJun.getName());
        setLevel(level);
        setId(monsterId);
        String life = xinJun.getLife_effect();
        if (life.contains("*")) {
            String[] strings = life.split("\\*");
            String[] ss = strings[1].split("\\+");
            int l = layer * Integer.parseInt(ss[0].trim()) + (ss.length > 1 ? Integer.parseInt(ss[1].trim()) : 0);
            setMaxLife(l * layer);
            setCurrLife(l * layer);
        } else {
            setMaxLife(Integer.parseInt(life) * layer);
            setCurrLife(Integer.parseInt(life) * layer);

        }
        setDefense(0);
        this.xinjun = xinJun;
        String speed = xinJun.getSpeed_effect();
        if (speed.contains("*")) {
            String[] strings = speed.split("\\*");
            String[] ss = strings[1].split("\\+");
            int l = layer * Integer.parseInt(ss[0].trim()) + (ss.length > 1 ? Integer.parseInt(ss[1].trim()) : 0);
            setSpeed((short) (l * layer));
        } else {
            setSpeed(Short.parseShort(speed));
        }

        String phy_effect = xinJun.getPhy_effect();
        if (phy_effect.contains("*")) {
            String[] strings = phy_effect.split("\\*");
            String[] ss = strings[1].split("\\+");
            int l = layer * Integer.parseInt(ss[0].trim()) + (ss.length > 1 ? Integer.parseInt(ss[1].trim()) : 0);
            setPhyPower((l * layer));
        } else {
            setPhyPower(Integer.parseInt(phy_effect) * layer);
        }
        setMonster(hostId > 0);
        setPolar((short) xinJun.getPolar());
        String skills = xinJun.getSkills();
        if (skills.contains(";")) {
            String[] strings = skills.split(";");
            Random random = new Random();
            int j = random.nextInt(strings.length);
            String temp = strings[j];
            skills = temp.substring(1);
            int polar = Integer.parseInt(temp.substring(0, 1));
            setPolar((short) polar);
            ArrayList<Integer> arrayList = new ArrayList<>(Json.fromJsonAsList(Integer.class, skills));
            setSkillIds(arrayList);
        } else {
            ArrayList<Integer> arrayList = new ArrayList<>(Json.fromJsonAsList(Integer.class, skills));
            setSkillIds(arrayList);
        }
        setIcon(npcIcon);
        setHostId(hostId);
    }


    @Override
    public int getMagHurt(FightObject target, boolean isPenetrate) {
        return Integer.parseInt(this.getXinjun().getMag_effect()) * this.getLevel() - (target.getDefense() + target.getExtraDefense() + this.getTargetResistValue(target) * 50);
    }

    @Override
    public int getPhyHurt(FightObject target, boolean isPenetrate) {
        return Integer.parseInt(this.getXinjun().getPhy_effect()) * this.getLevel() - target.getDefense() - target.getExtraDefense();
    }
}
